﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Reflection;


namespace BaseFunction
{
    public class DataTableHelp
    {
        /// <summary>
        /// 将DataRow[] 转换为Datatable
        /// </summary>
        /// <param name="drs">要转换的列</param>
        /// <param name="ds">目标数据源的架构</param>
        /// <returns></returns>
        public static DataTable DrToDt(DataRow[] drs, DataSet ds)
        {
            DataTable dt = ds.Tables[0].Clone();
            dt.Rows.Clear();
            for (int i = 0; i < drs.Length; i++)
            {
                dt.Rows.Add(drs[i].ItemArray);
            }
            return dt;

        }
        /// <summary>
        /// 将DataRow[] 转换为Datatable
        /// </summary>
        /// <param name="drs">要转换的列</param>
        /// <param name="ds">目标数据源的架构</param>
        /// <returns></returns>
        public static DataTable DrToDt(DataRow[] drs, DataTable dts)
        {
            DataTable dt = dts.Clone();
            dt.Rows.Clear();
            for (int i = 0; i < drs.Length; i++)
            {
                dt.Rows.Add(drs[i].ItemArray);
            }
            return dt;

        }
        /// <summary>
        /// 将DataRow[] 转换为Datatable
        /// </summary>
        /// <param name="drs">要转换的列</param>
        /// <param name="ds">目标数据源的架构</param>
        /// <returns></returns>
        public static DataTable ListToDt(List<DataRow> list_dr, DataTable dts)
        {
            DataTable dt = dts.Clone();
            dt.Rows.Clear();
            for (int i = 0; i < list_dr.Count; i++)
            {
                dt.Rows.Add(list_dr[i].ItemArray);
            }
            return dt;

        }
        /// <summary>
        /// 将集合类转换成DataTable
        /// </summary>
        /// <param name="list">集合</param>
        /// <returns></returns>
        public static DataTable ToDataTable(IList list)
        {
            DataTable result = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    result.Columns.Add(pi.Name, pi.PropertyType);
                }

                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }
            }
            return result;
        }

        /// <summary>
        /// 选取前几条数据 将DataRow[] 转换为Datatable
        /// </summary>
        /// <param name="drs">要转换的列</param>
        /// <param name="ds">目标数据源的架构</param>
        /// <returns></returns>
        public static DataTable DrToDt(DataRow[] drs, DataTable OldDt, int cout)
        {
            DataTable dt = OldDt.Clone();
            dt.Rows.Clear();
            if (drs.Length < cout)
                cout = drs.Length;

            for (int i = 0; i < cout; i++)
            {
                dt.Rows.Add(drs[i].ItemArray);
            }
            return dt;

        }
        /// <summary>
        /// 获取DataTable 多少条数据记录
        /// </summary>
        /// <param name="dt">Datable</param>
        /// <param name="Start">从位置开始</param>
        /// <param name="End">到多少号位置结束</param>
        /// <returns></returns>
        public static DataTable GetTopTable(DataTable dt, int Start, int End)
        {
            DataTable Newdt = dt.Clone();
            Newdt.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count ; i++)
            {
                if (i >= Start && i <= End)
                {
                    Newdt.Rows.Add(dt.Rows[i].ItemArray);
                }
            }
            return Newdt;
        }

        /// <summary>
        /// 获取DataTable 多少条数据记录
        /// </summary>
        /// <param name="dt">Datable</param>
        /// <param name="Count">获取多少条数据</param>
        /// <returns></returns>
        public static DataTable GetTopTable(DataTable dt, int Count)
        {
            DataTable Newdt = dt.Clone();
            Newdt.Rows.Clear();
            if (dt.Rows.Count < Count)
                Count = dt.Rows.Count;
            for (int i = 0; i < Count; i++)
            {
                Newdt.Rows.Add(dt.Rows[i].ItemArray);
            }
            return Newdt;
        }

        /// <summary> 
        /// DataTable分页 
        /// </summary> 
        /// <param name="dt">DataTable</param> 
        /// <param name="PageIndex">页索引,注意：从1开始</param> 
        /// <param name="PageSize">每页大小</param> 
        /// <returns>分好页的DataTable数据</returns>              第1页        每页10条 
        public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
        {
            if (PageIndex == 0) { return dt; }
            DataTable newdt = dt.Copy();
            newdt.Clear();
            int rowbegin = (PageIndex - 1) * PageSize;
            int rowend = PageIndex * PageSize;

            if (rowbegin >= dt.Rows.Count)
            { return newdt; }

            if (rowend > dt.Rows.Count)
            { rowend = dt.Rows.Count; }
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                DataRow newdr = newdt.NewRow();
                DataRow dr = dt.Rows[i];
                foreach (DataColumn column in dt.Columns)
                {
                    newdr[column.ColumnName] = dr[column.ColumnName];
                }
                newdt.Rows.Add(newdr);
            }
            return newdt;
        }

        /// <summary> 
        /// 返回分页的页数 
        /// </summary> 
        /// <param name="count">总条数</param> 
        /// <param name="pageye">每页显示多少条</param> 
        /// <returns>如果 结尾为0：则返回1</returns> 
        public static int PageCount(int count, int pageye)
        {
            int page = 0;
            int sesepage = pageye;
            if (count % sesepage == 0) { page = count / sesepage; }
            else { page = (count / sesepage) + 1; }
            if (page == 0) { page += 1; }
            return page;
        }



    }
}
